package com.pacy.swj.swjwb.Utils;

import android.os.Environment;

import java.io.File;
import java.io.IOException;

import h.framework.android.utils.LogUtil;

/**
 * @author PACY
 * 创建时间 19/6/10 16:14.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述
 */
public class FileUtil {
    /**
     * 获取SdCard路径
     *
     * @return
     */
    public static String getSDPath() {
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        File localFile = null;
        if (sdCardExist) {
            localFile = Environment.getExternalStorageDirectory();
        }
        if (localFile != null) {
            return localFile.toString();
        }
        return "/mnt/sdcard-ext";
    }


    /**
     * TODO<创建文件夹>
     *
     * @return File
     */
    public static File createDir(String path) {
        File dir = new File(path);
        if (!isExist(dir)) {
            dir.mkdirs();
        }
        return dir;
    }


    /**
     * TODO<创建文件>
     *
     * @return File
     */
    public static File createFile(String path, String fileName) {
        File file = new File(createDir(path), fileName);
        if (!isExist(file)) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                LogUtil.e("创建文件出错：" + e.toString());
                e.printStackTrace();
            }
        }
        return file;
    }


    /**
     * TODO<判断File对象所指的目录或文件是否存在>
     *
     * @return boolean
     */
    public static boolean isExist(File file) {
        return file.exists();
    }


    /**
     * 获取某个路径下的文件列表
     *
     * @param path 文件路径
     * @return 文件列表File[] files
     */
    public static File[] getFileList(String path) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                return files;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
